<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class PhotoUpload extends CI_Controller {
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function index() {
		$this->cismarty->assign ( 'baseUrl', $this->config->item ( 'base_url' ) );
		$this->cismarty->display ( 'fileUpload.tpl' );
	}
	
	public function upload() {
		$picname = $_FILES ['mypic'] ['name'];
		$picsize = $_FILES ['mypic'] ['size'];
		if ($picname != "") {
			if ($picsize > 1024 * 1024) { //限制上传大小 
				echo '图片大小不能超过500k';
				exit ();
			}
			$type = strstr ( $picname, '.' ); //限制上传格式 
			if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
				echo '图片格式不对！';
				exit ();
			}
			$rand = rand ( 100, 999 );
			$pics = date ( "YmdHis" ) . $rand . $type; //命名图片名称 
			//上传路径 
			$pic_path = $this->config->item ( 'photo_dir' ) . $pics;
			move_uploaded_file ( $_FILES ['mypic'] ['tmp_name'], $pic_path );
			//move_uploaded_file($_FILES["mypic"]["tmp_name"],"upload/" . $_FILES["mypic"]["name"]);
		}
		$size = round ( $picsize / 1024, 2 ); //转换成kb 
		$arr = array ('name' => $picname, 'fileName' => $pics, 'path' => $this->config->item ( 'photo_path' ).$pics, 'size' => $size );
		echo json_encode ( $arr ); //输出json数据 
	}
	
	public function delete() {
		$filename = $_POST ['imagename'];
		if (! empty ( $filename )) {
			unlink ( $this->config->item ( 'photo_dir' ) . $filename );
			echo '1';
		} else {
			echo '删除失败.';
		}
	}

}
	

